package com.example.blenderandroid;

import java.util.ArrayList;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class DiscoverBtDevicesActivity extends Activity {
	private ArrayList<BluetoothDevice> m_DiscoveredDevices;
	
	private ArrayAdapter<String> m_DiscoveredDevicesListAdapter;
	
	private final BroadcastReceiver m_BroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (BluetoothDevice.ACTION_FOUND.equals(action)) {
				BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				m_DiscoveredDevices.add(device);
				m_DiscoveredDevicesListAdapter.add(device.getName() + "\n" + device.getAddress());
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_discover_bt_devices);

		ListView pairedDevicesView = (ListView) findViewById(R.id.discovered_devices_view);

		m_DiscoveredDevices = new ArrayList<BluetoothDevice>();
		m_DiscoveredDevicesListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
		pairedDevicesView.setAdapter(m_DiscoveredDevicesListAdapter);
		
		IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
		registerReceiver(m_BroadcastReceiver, filter);
		
		pairedDevicesView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent connectBtIntent = new Intent(DiscoverBtDevicesActivity.this, ConnectBtActivity.class);
				
				// attach the address of the chosen bluetooth device to the intent
				BluetoothDevice selectedDevice = m_DiscoveredDevices.get(position);
				connectBtIntent.putExtra(ConnectBtActivity.BT_ADDRESS, selectedDevice.getAddress());
				
				DiscoverBtDevicesActivity.this.startActivity(connectBtIntent);
			}
		});
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		// initiate discovery of bluetooth devices
		m_DiscoveredDevices.clear();
		m_DiscoveredDevicesListAdapter.clear();
		BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		bluetoothAdapter.cancelDiscovery();
		bluetoothAdapter.startDiscovery();
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(m_BroadcastReceiver);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.discover_bt_devices, menu);
		return true;
	}
}
